layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 管理
     */
    var BtyArea = {
        tableId: "btyAreaTable"
    };

    initDm();
    /**
     * 初始化营运经理
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyPerformanceMonthInformation/listYyjl");
    var languageResult = languageAjax.start();
    $.each(languageResult.data, function( index, val ) {
        $("#yyjl").append("<option value='"+val.yyjl+"'>"+val.yyjl+"</option>");
    } );
    form.render('select');

    /**
     * 初始化督导
     */
    form.on('select(yyjl)', function (data) {
        var yyjl = data.value;
        if (yyjl == "") {
            $("#dd").html("");
            $("#dd").append("<option value=''>全部营运经理</option>");
            form.render('select');
        } else {
            var qyAjax = new $ax("/btyArea/ddList");
            qyAjax.set("yyjl", yyjl);
            var qyResult = qyAjax.start();
            $("#dd").html("");
            $("#dd").append("<option value=''>全部督导</option>");
            $.each(qyResult.data, function (index, data) {
                $("#dd").append("<option value='" + data.dd + "'>" + data.dd + "</option>");
            });
            form.render('select');
        }

        if (yyjl == "") {
            initDm();
        } else {
            var qyAjax = new $ax("/btyArea/getShopByYyjl");
            qyAjax.set("yyjl", yyjl);
            var qyResult = qyAjax.start();
            $("#dm1").html("");
            $("#dm1").append("<option value=''>全部门店</option>");
            $.each(qyResult.data, function (index, data) {
                $("#dm1").append("<option value='" + data.dm + "'>" + data.dm + "</option>");
            });
            form.render('select');
        }
        $('#dm').val('');
        BtyArea.search();
    });

    /**
     * 选择督导
     */
    form.on('select(dd)', function (data) {
        var dd = data.value;
        var yyjl = $('#yyjl').val();

        var qyAjax = new $ax("/btyArea/getShopByYyjl");
        qyAjax.set("yyjl", yyjl);
        qyAjax.set("dd", dd);
        var qyResult = qyAjax.start();
        $("#dm1").html("");
        $("#dm1").append("<option value=''>全部门店</option>");
        $.each(qyResult.data, function (index, data) {
            $("#dm1").append("<option value='" + data.dm + "'>" + data.dm + "</option>");
        });
        form.render('select');
        $('#dm').val('');
        BtyArea.search();
    });

    /**
     * 选择督导
     */
    form.on('select(dm)', function (data) {
        var dm = data.value;
        $('#dm').val(dm);
        BtyArea.search();
    });

    /**
     * 选择是否合规
     */
    form.on('select(isCompliance)', function (data) {
        var isCompliance = data.value;
        $('#isCompliance').val(isCompliance);
        BtyArea.search();
    });

    /**
     * 初始化店名
     */
    function initDm()
    {
        var languageAjax = new $ax(Feng.ctxPath + "/btyWage/getDm");
        var languageResult = languageAjax.start();
        $.each(languageResult, function( index, val ) {
            $("#dm1").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
        } );
        form.render('select');
    }

    /**
     * 初始化表格的列
     */
    BtyArea.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'bh', title: '编号', width: 150},
            {field: 'dm', title: '店名'},
            {field: 'yyzzmc', title: '营业执照名称'},
            {field: 'yyjl', title: '营运经理'},
            {field: 'dd', title: '督导'},
            // {field: 'xm', sort: true, title: ''},
            // {field: 'id', hide: true, title: ''},
            // {field: 'bz', sort: true, title: ''},
            // {field: 'flag', sort: true, title: ''},
            // {field: 'gz', sort: true, title: ''},
            // {field: 'flagd', sort: true, title: ''},
            // {field: 'qtgz', sort: true, title: ''},
            // {field: 'qt', sort: true, title: ''},
            // {field: 'dz', sort: true, title: ''},
            // {field: 'dh', sort: true, title: ''},
            // {field: 'kqDz', sort: true, title: ''},
            // {field: 'kqDh', sort: true, title: ''},
            // {field: 'yydh', sort: true, title: ''},
            // {field: 'dddh', sort: true, title: ''},
            // {field: 'dhTels', sort: true, title: ''},
            // {field: 'kqDhTels', sort: true, title: ''},
            // {field: 'yydhTels', sort: true, title: ''},
            // {field: 'dddhTels', sort: true, title: ''},
            {field: 'isCompliance', title: '是否合规', templet: function(data) {
                var val = data.isCompliance == 0 || data.isCompliance == "" ? 0 : 1;
                if (val === 0) {
                    return `<input value="${val}" lay-filter="isComplianceSwitch" data-id="${data.id}" type="checkbox" lay-skin="switch" lay-text="合规|不合规" checked>`;
                } else {
                    return `<input value="${val}" lay-filter="isComplianceSwitch" data-id="${data.id}" type="checkbox" lay-skin="switch" lay-text="合规|不合规">`;
                }
                   
            }},
            // {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    $('.batchEditStatus').click(function() {
        var checkStatus = table.checkStatus(BtyArea.tableId);
        var data = checkStatus.data;

        var ids = [];
        for (var i = 0; i < data.length; i++) {
            ids.push(data[i].id);
        }
        if (ids.length == 0) {
            Feng.error("请选择数据");
            return;
        }
        var status = $(this).data('status');

        let switchCalue = data.value == 0 ? 1 : 0;
        var ajax = new $ax(Feng.ctxPath + "/btyArea/batchSwitchStatus", function (data) {
            Feng.success("修改成功!");
            table.reload(BtyArea.tableId);
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids.join(','));
        ajax.set("isCompliance", status);
        ajax.start();

    })


    form.on('switch(isComplianceSwitch)', function(data){
        let switchCalue = data.value == 0 ? 1 : 0;
        var ajax = new $ax(Feng.ctxPath + "/btyArea/switchStatus", function (data) {
            Feng.success("修改成功!");
            $(this).val(switchCalue)
            // table.reload(BtyArea.tableId);
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", $(this).data('id'));
        ajax.set("isCompliance", switchCalue);
        ajax.start();
    });
    /**
     * 点击查询按钮
     */
    BtyArea.search = function () {
        var queryData = {};
        queryData['isCompliance'] = $('#isCompliance').val();
        queryData['yyjl'] = $('#yyjl').val();
        queryData['dd'] = $('#dd').val();
        queryData['dm'] = $('#dm').val();
        // if ($('#dm1').val() != '') {
        //     queryData['dm'] = $('#dm').val();
        // }
        // if ($('#dm').val() != '') {
        //     queryData['dm'] = $('#dm').val();
        // }

        table.reload(BtyArea.tableId, {
            where: queryData, page: {curr: 1, limit: 20}
        });
    };

    /**
     * 跳转到添加页面
     */
    BtyArea.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/btyArea/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    BtyArea.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/btyArea/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    BtyArea.exportExcel = function () {
        var checkRows = table.checkStatus(BtyArea.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyArea.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyArea/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyArea.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyArea.tableId,
        url: Feng.ctxPath + '/btyArea/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        limit: 20,
        cols: BtyArea.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyArea.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyArea.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyArea.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyArea.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyArea.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            BtyArea.onDeleteItem(data);
        }
    });
});
